/*
 *   Copyright 2012, Thomas Kerber
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package milk.jpatch.classLevel;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import milk.jpatch.CPoolMap;
import milk.jpatch.Patch;

import org.apache.bcel.classfile.Field;

/**
 * Denotes a patch of the field list.
 * @author Thomas Kerber
 * @version 1.0.0
 */
public class FieldsPatch implements Patch<List<Field>>{
    
    private static final long serialVersionUID = 3456062281608450043L;
    
    /**
     * The component patches.
     */
    private List<FieldPatch> patches;
    
    /**
     * Creates.
     * @param patches The component patches.
     */
    public FieldsPatch(List<FieldPatch> patches){
        this.patches = patches;
    }
    
    /**
     * Generates.
     * @param old The old fields.
     * @param new_ The new fields.
     * @param map The cpool map.
     * @return The patch.
     */
    public static FieldsPatch generate(Field[] old, Field[] new_, CPoolMap map){
        List<FieldPatch> patches = new ArrayList<FieldPatch>();
        
        Set<String> names = new HashSet<String>();
        
        for(Field f : old)
            names.add(f.getName());
        for(Field f : new_)
            names.add(f.getName());
        
        for(String name : names){
            Field oldF = null;
            Field newF = null;
            for(Field f : old){
                if(f.getName().equals(name)){
                    oldF = f;
                    break;
                }
            }
            for(Field f : new_){
                if(f.getName().equals(name)){
                    newF = f;
                    break;
                }
            }
            
            if(oldF == null && newF == null)
                continue; // Theoretically impossible, but hey.
            else if(oldF == null){
                patches.add(FieldAdd.generate(newF));
            }
            else if(newF == null){
                patches.add(FieldRem.generate(oldF));
            }
            else{
                patches.add(FieldModif.generate(oldF, newF, map));
            }
        }
        return new FieldsPatch(patches);
    }
    
    /**
     * 
     * @return The component patches.
     */
    public List<FieldPatch> getPatches(){
        return patches;
    }
    
    @Override
    public List<Field> patch(List<Field> fields, CPoolMap map){
        for(FieldPatch p : patches)
            fields = p.patch(fields, map);
        return fields;
    }
    
}
